package com.ruoyi.disCustomer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.disCustomer.domain.TCustomerDisRecord;
import com.ruoyi.disCustomer.domain.bo.TCustomerDisRecordBo;
import com.ruoyi.disCustomer.domain.vo.TCustomerDisRecordVo;
import com.ruoyi.disCustomer.mapper.TCustomerDisRecordMapper;
import com.ruoyi.disCustomer.service.ITCustomerDisRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 客户分配记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-20
 */
@RequiredArgsConstructor
@Service
public class TCustomerDisRecordServiceImpl implements ITCustomerDisRecordService {

    private final TCustomerDisRecordMapper baseMapper;

    /**
     * 查询客户分配记录
     */
    @Override
    public TCustomerDisRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户分配记录列表
     */
    @Override
    public TableDataInfo<TCustomerDisRecordVo> queryPageList(TCustomerDisRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TCustomerDisRecord> lqw = buildQueryWrapper(bo);
        Page<TCustomerDisRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户分配记录列表
     */
    @Override
    public List<TCustomerDisRecordVo> queryList(TCustomerDisRecordBo bo) {
        LambdaQueryWrapper<TCustomerDisRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TCustomerDisRecord> buildQueryWrapper(TCustomerDisRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TCustomerDisRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCustomerId() != null, TCustomerDisRecord::getCustomerId, bo.getCustomerId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusType()), TCustomerDisRecord::getBusType, bo.getBusType());
        lqw.eq(bo.getOperateDisId() != null, TCustomerDisRecord::getOperateDisId, bo.getOperateDisId());
        lqw.like(StringUtils.isNotBlank(bo.getOperateDisName()), TCustomerDisRecord::getOperateDisName, bo.getOperateDisName());
        lqw.eq(bo.getOperateDeptId() != null, TCustomerDisRecord::getOperateDeptId, bo.getOperateDeptId());
        lqw.eq(bo.getDisDeptId() != null, TCustomerDisRecord::getDisDeptId, bo.getDisDeptId());
        lqw.eq(bo.getDisUserId() != null, TCustomerDisRecord::getDisUserId, bo.getDisUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getDisReason()), TCustomerDisRecord::getDisReason, bo.getDisReason());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), TCustomerDisRecord::getCustomerName, bo.getCustomerName());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), TCustomerDisRecord::getTel, bo.getTel());
        return lqw;
    }

    /**
     * 新增客户分配记录
     */
    @Override
    public Boolean insertByBo(TCustomerDisRecordBo bo) {
        TCustomerDisRecord add = BeanUtil.toBean(bo, TCustomerDisRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户分配记录
     */
    @Override
    public Boolean updateByBo(TCustomerDisRecordBo bo) {
        TCustomerDisRecord update = BeanUtil.toBean(bo, TCustomerDisRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TCustomerDisRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户分配记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
